package com.yzycoc.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;

/**
 * 文件处理
 *
 * @author: 会跑的仓鼠
 * 1.0
 **/
public class FileUtils {


    static class get{
        /**
         * 读取文件
         * @param filePath
         * @return
         */
        public static String txt(String filePath)  {
            String info = null;
            try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
                /**
                 * 获取文件指定的文件信息
                 * 将数据读到字节数组里
                 */
                byte[] buff = new byte[1024];
                int length = fileInputStream.read(buff);
                /**
                 * 3.将字节数据转换为字符串
                 * 参数一：带转换的字节数组，参数二：起始位置  参数三：转换的长度
                 */
                info = new String(buff, 0, length);
                System.out.println(info);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return info;
        }

        /***
         * 获取图片
         * @param filePath
         * @return 图片
         */
        public BufferedImage image(String filePath) {
            File file = new File(filePath);
            BufferedImage read = null;
            if (!file.exists()) {
                return null;
            }
            try {
                read = ImageIO.read(file);
                return read;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }


    static class set{
        /**
         * 写入文本内容
         * @param filepath 路径
         * @param content 内容
         * @throws IOException 错误
         */
        public static void setTxt(String filepath, String content){
            try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filepath))) {
                bufferedWriter.write(content);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    static class remove{
        /***
         * 删除指定文件夹下所有文件
         * 删除两个小时钱的文件夹
         * @param path 文件夹完整绝对路径
         * @param date 删除两个小时钱的文件夹
         * @return 逻辑
         */
        public static  boolean delAllFile(String path, Date date) {
            boolean flag = false;
            File file = new File(path);
            if (!file.exists()) {
                return false;
            }
            if (!file.isDirectory()) {
                return false;
            }
            String[] tempList = file.list();
            File temp = null;
            for (int i = 0; i < tempList.length; i++) {
                if (path.endsWith(File.separator)) {
                    temp = new File(path + tempList[i]);
                } else {
                    temp = new File(path + File.separator + tempList[i]);
                }
                if (temp.isFile()) {
                    if (new Date(temp.lastModified()).before(date)) {
                    }
                    temp.delete();
                }
                if (temp.isDirectory()) {
                    //delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
                    flag = true;
                }
            }
            return flag;
        }
    }


}
